package com.hardroid.workload.ui.fragment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.hardroid.workload.R;

import com.hardroid.workload.ui.tablet.DashboardMultiPaneActivity;
import com.hardroid.workload.utils.HardroidUtils;
import com.hardroid.workload.utils.Singleton;
import com.hardroid.workload.utils.UIUtils;

public class OpenProjectFragment extends ListFragment {

	private static final int DELETE_PROJECT = 0;	
	
	public static final int TIME_PICKER_START_DIALOG = 2;
	public static final int TIME_PICKER_END_DIALOG = 1;
	
	boolean dualPane = false;
	public List<String> directory;
	public ArrayAdapter<String> adapter;

	public int selectedPos = -999;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		File currDir = new File("/sdcard");
		File[] files = currDir.listFiles();
		directory = new ArrayList<String>();

		if (files != null)
			for (File f : files) {
				if (f.isFile()) {
					if (Singleton.filterExtension(f))
						directory.add(f.toString());
				}
			}

		if (!directory.isEmpty()) {
			adapter = new ArrayAdapter<String>(getActivity(),
					android.R.layout.simple_list_item_single_choice, directory);
			setListAdapter(adapter);
		} else {
			System.out.println("directory is empty");
		}

//		dualPane = !(getActivity() instanceof OpenProjectActivity);
		dualPane = true;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_open_project, container,
				false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		// ListView listView =
		// (ListView)getActivity().findViewById(android.R.id.list);
		// listView.setChoiceMode(listView.CHOICE_MODE_SINGLE);
		registerForContextMenu(getListView());

	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		selectedPos = position;
		openItemList();
	}

	public void openItemList() {
		if (dualPane) {
			getListView().setItemChecked(selectedPos, true);
		} else {
			openProject();			
			getListView().setItemChecked(selectedPos, true);
		}

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Menu");
		menu.add(0, DELETE_PROJECT, 0, "Delete project");
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		selectedPos = info.position;
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == DELETE_PROJECT) {
			boolean retval = false;
			String removed = directory.remove(selectedPos);
			File removedFile = new File(removed);
			if (removedFile.exists()) {
				retval = removedFile.delete();
				adapter.notifyDataSetChanged();
			}
			return retval;
		}
		return false;
	}

	//
	// @Override
	// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long
	// arg3) {
	// // TODO Auto-generated method stub
	// // super.on
	// selected_position = arg2;
	// Toast.makeText(getActivity(), arg2+"", Toast.LENGTH_SHORT).show();
	// }
	//

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		File currDir = new File("/sdcard");
		File[] files = currDir.listFiles();

		if (files != null)
			directory.clear();

		if (files != null) {

			for (File f : files) {
				if (f.isFile()) {
					if (Singleton.filterExtension(f))
						directory.add(f.toString());
				}
			}

		}
		if (adapter != null)
			adapter.notifyDataSetChanged();
	}

	public void openProject() {
		Intent i = null;
		if (UIUtils.isHoneycombTablet(getActivity())) {
			i = new Intent(getActivity(), DashboardMultiPaneActivity.class);

		} else {
//			i = new Intent(getActivity(), DashboardActivity.class);
		}

		try {

			try {
				File file = new File(directory.get(selectedPos));
				
				Singleton.loadProject(getActivity().getApplicationContext(),
						file);				
				System.out.println(file + " opened ");
				
				Log.i("Open Project", "Singleton check, is null " +(Singleton.todayReference==null)+"");
				if (Singleton.todayReference!=null){
					Log.i("Singleton", HardroidUtils.dateToSDate(Singleton.todayReference.obsvDate)+"");	
				}
				
				// pas dibuka, kalo day reference belum, minta
//				if (Singleton.loadDayReference()){
//					getActivity().finish();
//					startActivity(i);	
//				}else{
//					getActivity().showDialog(OpenProjectFragment.TIME_PICKER_START_DIALOG);
//				}	
//				
				getActivity().finish();
				startActivity(i);	
								
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(getActivity(), "Cannot open project",
					Toast.LENGTH_SHORT).show();

		}
	}
	
	

}